package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRuleDistrict;
import com.pubinfo.passbook.common.entity.TRuleFrequency;
import com.pubinfo.passbook.common.entity.TSystemRole;
import com.pubinfo.passbook.common.mapper.TRuleDistrictMapper;
import com.pubinfo.passbook.common.mapper.TRuleFrequencyMapper;
import com.pubinfo.passbook.common.model.dto.rule.RuleFrequencyParam;
import com.pubinfo.passbook.common.model.http.eip.UserInfo;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.master.TRuleFrequencyService;
import com.pubinfo.passbook.common.service.ruler.CheckService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.pubinfo.passbook.common.model.constant.RoleEnum.ADMIN;
import static com.pubinfo.passbook.common.model.constant.RoleEnum.AUDITOR;

/**
 *
 */
@Service
public class TRuleFrequencyServiceImpl extends ServiceImpl<TRuleFrequencyMapper, TRuleFrequency>
    implements TRuleFrequencyService {
  
  @Resource
  private CheckService userInfoService;
  @Resource
  private TRuleDistrictMapper districtMapper;
  
  @Override
  public IPage<TRuleFrequency> listRuleFrequency(RuleFrequencyParam param, UserInfo userInfo, TSystemRole systemRole) {
    IPage<TRuleFrequency> iPage = page(param.getPage());
    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
      param.setDistrictId(systemRole.getDistrictId());
      return this.baseMapper.listRuleFrequency(iPage, param);
    }
    if (userInfoService.judgeRole(ADMIN.getRoleCode(), userInfo))
      return this.baseMapper.listRuleFrequency(iPage, param);
    return new Page();
  }
  
  @Override
  public JsonResult<String> removeRuleFrequency(String id, UserInfo userInfo, TSystemRole systemRole) {
    TRuleFrequency tRuleFrequency = this.baseMapper.selectById(id);
    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
      if (!systemRole.getDistrictId().equals(tRuleFrequency.getDistrictId()))
        return JsonResult.error("只能修改所属区县的记录");
    }
    this.baseMapper.deleteById(id);
    return JsonResult.success("删除成功");
  }
  
  @Override
  public JsonResult<String> updateRuleFrequency(TRuleFrequency frequency, UserInfo userInfo, TSystemRole systemRole) {
    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
      if (!systemRole.getDistrictId().equals(frequency.getDistrictId()))
        return JsonResult.error("只能修改所属区县的记录");
    }
    TRuleDistrict tRuleDistrict = districtMapper.selectById(frequency.getDistrictId());
    if (tRuleDistrict == null) {
      return JsonResult.error("对应区县不存在");
    }
    this.baseMapper.updateById(frequency);
    return JsonResult.success("修改成功");
  }
  
  @Override
  public JsonResult<String> saveRuleFrequency(TRuleFrequency frequency, UserInfo userInfo, TSystemRole systemRole) {
    if (userInfoService.judgeRole(AUDITOR.getRoleCode(), userInfo)) {
      if (!systemRole.getDistrictId().equals(frequency.getDistrictId()))
        return JsonResult.error("只能修改所属区县的记录");
    }
    TRuleDistrict tRuleDistrict = districtMapper.selectById(frequency.getDistrictId());
    if (tRuleDistrict == null) {
      return JsonResult.error("对应区县不存在");
    }
    this.baseMapper.insert(frequency);
    return JsonResult.success("添加成功");
  }
}




